package day1015;

import java.util.Stack;

/**
 * @Author:galaxy_yan
 * @Date: 2022/10/17
 * @Time: 20:21
 * @Description
 */
public class MinStack {
    Stack<Integer> stack;
    Stack<Integer> ministack;
    public MinStack() {
        stack=new Stack<>();
        ministack=new Stack<>();
    }

    public void push(int val) {
        stack.push(val);
        if(ministack.isEmpty()||val<ministack.peek()){
            ministack.push(val);
        }
    }

    public void pop() {
        if(stack.isEmpty()){
            return;
        }
        if(!ministack.isEmpty()&&stack.peek()==ministack.peek()){
            ministack.pop();
        }
        stack.pop();
    }


    public int top() {
        if(stack.isEmpty()){
            return -1;
        }
        return stack.peek();
    }

    public int getMin() {
        if(ministack.isEmpty()){
            return -1;
        }
        return ministack.peek();
    }
}
